package com.bjtg.threegoclinet.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.os.Bundle;

import com.bjtg.threegoclinet.MainActivity;
import com.bjtg.threegoclinet.bean.ConstValue;
import com.bjtg.threegoclinet.operation.DoorOperation;
import com.bjtg.threegoclinet.util.IsInternet;
import com.bjtg.threegoclinet.util.LattePreference;
import com.event.OpenDoorType;
import com.log.LatteLogger;

import org.json.JSONException;
import org.json.JSONObject;

import cn.jpush.android.api.JPushInterface;

import static com.bjtg.threegoclinet.bean.ConstValue.JPUSH_EXTRA;
import static com.bjtg.threegoclinet.bean.ConstValue.STAFF_SCAN_ID;
import static com.bjtg.threegoclinet.bean.ConstValue.ScanType;
import static com.bjtg.threegoclinet.bean.ConstValue.USER_SCAN_ID;

/**
 * 推送公共类
 * 日期:2017/04/21
 */
public class ThreeGoReceiver extends BroadcastReceiver {
    private static final String TAG = "ThreeGoReceiver";

    static final String ACTION = "android.intent.action.BOOT_COMPLETED";

    @Override
    public void onReceive(Context context, Intent intent) {

        LatteLogger.d(TAG, "BootReceiver.onReceive: " + intent.getAction() + "," + intent.getDataString());

        String action = intent.getAction();
        if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION) ||
                action.equals(JPushInterface.ACTION_CONNECTION_CHANGE)) {
            isNetChange(context, action);
        } else if (action.equals(ACTION)) {
            openMainActivity(context, action);
        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(action)) {

            Bundle bundle = intent.getExtras();

            openDoor(action, bundle);
        }
    }

    private void isNetChange(Context context, String action) {
        // 如果相等的话就说明网络状态发生了变化
        if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION) ||
                action.equals(JPushInterface.ACTION_CONNECTION_CHANGE)) {
            IsInternet.checkNetwork(context);
        }
    }

    public static String userScanId;
    public static String staffScanId;
    public static String type;

    private void openDoor(String action, Bundle bundle) {
        LatteLogger.d(TAG, bundle);

        if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(action)) {

            try {
                String extra = (String) bundle.get(JPUSH_EXTRA);
                JSONObject json = new JSONObject(extra);
                type = json.getString(ConstValue.TYPE);
                String scanType = null;
                try {
                    scanType = json.getString(ScanType);
                } catch (Exception ex) {
                    LatteLogger.d(TAG, "qr open ..............");
                }
                LatteLogger.d(TAG, "scanType = " + scanType + "   type = " + type);

                if (OpenDoorType.BUYER.getType().equals(type)) {
                    buyGoodsTask(json, scanType);
                } else if (OpenDoorType.OPERATIONG.getType().equals(type)) {
                    addGoodsTask(json, scanType);
                } else if (OpenDoorType.CURENNTCLOSE.getType().equals(type)) {
                    //紧急关门(暂未开通)
                    LatteLogger.d(TAG, "收到服务端通知:C");
                }

            } catch (JSONException e) {
                e.printStackTrace();
                LatteLogger.d(TAG, "onReceive exception....");
                resetOpenDoorTime();

            }
        }
    }

    public static void resetOpenDoorTime() {
        LattePreference.addUserId(LattePreference.USER_ID, "1");
        LattePreference.addOperationId(LattePreference.OPERATION_ID, "1");
    }

    private void addGoodsTask(JSONObject json, String scanType) throws JSONException {
        LatteLogger.d(TAG, "收到服务端通知:O");
        staffScanId = json.getString(STAFF_SCAN_ID);
        if (LattePreference.getOperationId(LattePreference.OPERATION_ID).equals("1")) {
            LattePreference.addOperationId("staffScanId", staffScanId);
            LatteLogger.d(TAG, "staffScanId = " + staffScanId);
//            if (ConstValue.DOORTYPE.equals(DoorType.SINGLEDOOR.getDoorType())) {
//                OperationsInterface.operationsOpenDoor(staffScanId);
//            } else {
//                DoubleOperationsInterface.operationsOpenDoor(staffScanId);
//            }
            DoorOperation.openDoor(staffScanId, scanType);
        }
    }

    private void buyGoodsTask(JSONObject json, String scanType) throws JSONException {
        LatteLogger.d(TAG, json.toString());
        userScanId = json.getString(USER_SCAN_ID);
        LatteLogger.d(TAG, "收到服务端通知:B");
        if (LattePreference.getUserId(LattePreference.USER_ID).equals("1")) {
            LattePreference.addUserId(LattePreference.USER_ID, userScanId);
            LatteLogger.d(TAG, "userId = " + userScanId);
            DoorOperation.openDoor(userScanId, scanType);
//            if (ConstValue.DOORTYPE.equals(DoorType.SINGLEDOOR.getDoorType())) {
//                LatteLogger.d(TAG, "buyer open");
//                UserInterface.userOpenDoor(userScanId, scanType);
//            } else {
//                LatteLogger.d(TAG, "buyer open");
//                DoubleUserInterface.userOpenDoor(userScanId, scanType);
//            }
        }
    }

    private void openMainActivity(Context context, String action) {
        /**
         *
         * 开机自启动，显示主界面
         *
         */
        if (action.equals(ACTION)) {
            Intent mainActivityIntent = new Intent(context, MainActivity.class);  // 要启动的Activity
            mainActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(mainActivityIntent);
        }
    }


}
